package it.rap.applications.persistence.entities.filters;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Version;

@Entity
public class FilterTree extends TreeObject  implements Comparable<FilterTree>{

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	private BigDecimal id;
	private String description;
	@Column(nullable = false)
	private Boolean checked = false;
	@Version
	private Long versione;
	@OneToMany( cascade = CascadeType.ALL, orphanRemoval=true, mappedBy="filterTree")
	private List<RoleTree> roleTreeList = new ArrayList<RoleTree>();

	public FilterTree(BigDecimal idFilter, String name,	String filterDescription) {
		super(name);
		this.id = idFilter;
		this.description = filterDescription;
	}
	
	public FilterTree(String name, String filterDescription) {
		super(name);
		this.description = filterDescription;
	}

	public FilterTree(FilterTree f) {
		super(f.getName());
		this.id = f.getId();
		this.description = f.getDescription();
		this.checked = f.isChecked();
	}
	
	public FilterTree() {
		super();
	}
	public BigDecimal getId() {
		return id;
	}
	public void setId(BigDecimal id) {
		this.id = id;
	}	
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Boolean isChecked() {
		return checked;
	}
	public void setChecked(Boolean checked) {
		this.checked = checked;
	}
	public Long getVersione() {
		return versione;
	}
	public void setVersione(Long versione) {
		this.versione = versione;
	}
	public List<RoleTree> getRoleTreeList() {
		return roleTreeList;
	}
	public void setRoleTreeList(List<RoleTree> roleTreeList) {
		this.roleTreeList = roleTreeList;
	}
	public void addRole(RoleTree r) {
		if ( roleTreeList == null ){
			roleTreeList = new ArrayList<RoleTree>();
		}
		roleTreeList.add( r );
	}
	
	public boolean equals(FilterTree f) {
		return  ( ( id == null && f.getId() == null ) || ( id != null && id == f.getId() )) &&
				( this.getName() != null &&  this.getName().equalsIgnoreCase( f.getName() ) )	&&
				( description != null && description.equalsIgnoreCase( f.getDescription() ) ) &&
				( checked != null && checked == f.isChecked() );
		}
	public int compareTo(FilterTree f) {
		if ( this.id == null ) return -1;
		if ( this.id == f.getId()  ){
				return 0;
		}
		return -1;
	}
	public RoleTree getRoleTree(RoleTree r) {
		if ( roleTreeList == null ) return null;
		for ( RoleTree role: roleTreeList ){
			if ( role.getId() == r.getId() ){
				return role;
			}
		}
		return null;
	}
	 
	public String toString() {
		StringBuffer buf = new StringBuffer("");
		buf.append( "Filter(" + getId() + ", " + getName() + ", " + getDescription() + isChecked() + ")" );
		if ( roleTreeList != null && roleTreeList.size() > 0){
			for(RoleTree j: roleTreeList ){
				buf.append( j.toString() );
			}
		}
		return buf.toString();
	}

		
}
